# -*- coding: utf-8 -*-
'''
@今日头条热点新闻
'''
import subprocess
from common.config import *
import time
from selenium import webdriver
from random import randint
class Operations(object):
    def __init__(self):

        pass
    # 拖动浏览器滚动条
    def pullScrollOperation(self, headless=None,num=100, max_time=20):
        options = webdriver.FirefoxOptions()
        # 设置火狐无头模式
        if headless == "--headless":options.add_argument(headless)
        else :
            pass
        # 配置代理服务
        profile = webdriver.FirefoxProfile()
        profile.set_preference('network.proxy.type', 1)
        profile.set_preference('network.proxy.http', "127.0.0.1")
        profile.set_preference('network.proxy.http_port', 8080)  # int
        profile.set_preference('network.proxy.ssl', "127.0.0.1")
        profile.set_preference('network.proxy.ssl_port', 8080)
        profile.update_preferences()
        browser = webdriver.Firefox(firefox_options=options,firefox_profile=profile)

        try:
            # 访问今日头条热点地址
            browser.get(urls["domain"] + "/ch/news_hot/")

        except Exception as e:

            print("/*Network disconnect trying to refresh browser*/")

            return False

        for x in range(num):

            time.sleep(randint(10, max_time))
            # 下拉JS
            scroll = "window.scrollTo(0, document.body.scrollHeight)"

            browser.execute_script(scroll)
        #关闭浏览器
        browser.close()

    # 执行监听程序
    def executeListen(self):
        # 执行监听程序
        CMD = "mitmdump -s listen/mitmProxy.py"
        subprocess.call(CMD)
